﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayer.Model.Repositories.Abstract;
using BusinessLayer.Model.Services.Abstract;
using BusinessLayer.Model.Services.Concrete;
using BusinessLayer.Model.Entities;
using System.Globalization;
using DataAccess.Repositories;

namespace intecnologia.Admin
{
    public partial class EditEvents : System.Web.UI.Page
    {
        protected string _pageMode;

        private IEventRepository _eventRepository = new EventRepository();
        private ISessionService _sessionService = new SessionService();

        private Event _eventToAddorEdit;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (_sessionService.SessionExpired())
                Response.Redirect("~/Admin/Default.aspx");

            _pageMode = Request.QueryString["edit"] == "true" ? "edit" : "add";

            if (!IsPostBack)
                LoadTimeDropDownLists();

            if (_pageMode == "edit")
            {
                _eventToAddorEdit = _eventRepository.FetchByID(new Guid(Request.QueryString["id"]));

                if (!IsPostBack)
                    LoadEventDetails();
            }

            else if (_pageMode == "add")
                _eventToAddorEdit = new Event();               

        }

        protected void btnSubmitChanges_Click(object sender, EventArgs e)
        {
            _eventToAddorEdit.Name = tbName.Text;
            _eventToAddorEdit.Description = tbDescription.Text;
            _eventToAddorEdit.Place = tbPlace.Text;

            GetEventDate();

            if (_pageMode == "edit")
                _eventRepository.UpdateOnSubmit(_eventToAddorEdit);

            else if (_pageMode == "add")
                _eventRepository.InsertOnSubmit(_eventToAddorEdit);

            _eventRepository.SubmitChanges();

            ExitEdit();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ExitEdit();
        }


        private void GetEventDate()
        {
            CultureInfo provider = CultureInfo.InvariantCulture; //no varia y usa el formato que yo le especifique.

            //dia/mes/año hora:minutos am/pm
            string dateFormat = "dd/MM/yyyy h:mm tt";
            string date = tbEventDate.Text + " " + ddlHour.SelectedItem.Text +
                          ":" + ddlMinutes.SelectedItem.Text + " " + ddlTimeOfDay.SelectedItem.Text;

            _eventToAddorEdit.Date = DateTime.ParseExact(date, dateFormat, provider);
        }

        private void SetEventDate(Event e)
        {
            tbEventDate.Text = e.Date.Date.ToShortDateString();
            ddlHour.SelectedValue = e.Date.Hour.ToString();
            ddlMinutes.SelectedValue = e.Date.Minute.ToString();
            ddlTimeOfDay.SelectedValue = (e.Date.TimeOfDay > TimeSpan.FromHours(12)) ? "PM" : "AM"; 
        }


        private void LoadTimeDropDownLists()
        {
            ddlHour.DataSource = new string[12]
                                     {
                                         "01", "02", "03", "04", "05", "06",
                                         "07", "08", "09", "10", "11", "12"
                                     };
            ddlHour.DataBind();

            string[] minutes = new string[60];

            for (int m = 0; m != 60; ++m)
            {
                minutes[m] = (m < 10) ? minutes[m] = "0" + m :
                                        minutes[m] = m.ToString();

            }

            ddlMinutes.DataSource = minutes;
            ddlMinutes.DataBind();

            ddlTimeOfDay.DataSource = new string[2] { "PM", "AM" };
            ddlTimeOfDay.DataBind();
        }


        private void LoadEventDetails()
        {
            tbName.Text = _eventToAddorEdit.Name;
            tbDescription.Text = _eventToAddorEdit.Description;
            tbPlace.Text = _eventToAddorEdit.Place;
            SetEventDate(_eventToAddorEdit);
        }

        private void ExitEdit()
        {
            Response.Redirect("~/Admin/Events.aspx");
        }
    }
}
